package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 用户基础信息的dubbo实现类
 */
@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;


    //保存
    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
       return userInfoMapper.selectById(id);
    }

    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIds, UserInfo userInfo) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        //1.用户id列表
        queryWrapper.in("id",userIds);
        //2.添加筛选条件
        if (userInfo != null){
            if (userInfo.getAge() != null){
                queryWrapper.lt("age",userInfo.getAge());
            }
            if (!StringUtils.isEmpty(userInfo.getGender())){
                queryWrapper.eq("gender",userInfo.getGender());
            }
            if (!StringUtils.isEmpty(userInfo.getNickname())){
                queryWrapper.like("nickname",userInfo.getNickname());
            }
        }
        List<UserInfo> list = userInfoMapper.selectList(queryWrapper);
        Map<Long,UserInfo> map = CollUtil.fieldValueMap(list,"id");
        return map;
    }

    @Override
    public void updateUserHead(Long userId, String photo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId",userId);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        userInfo.setAvatar(photo);
        userInfoMapper.update(userInfo,queryWrapper);
    }

    @Override
    public IPage<UserInfo> findAll(Integer page, Integer pagesize) {
        return userInfoMapper.selectPage(new Page<UserInfo>(page,pagesize),null);
    }
}